package pencil.controller;

import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import pencil.service.pencil_service;
import pencil.to.factoryMaterial2TO;
import pencil.to.holeMachineTO;
import pencil.to.holeNamuTO;
import pencil.to.mixMachineTO;
import pencil.to.pencilTO;
import pencil.util.pencilUtil;

@Controller
@RequestMapping("/holeMachine")
public class holeMachineController {
private pencil_service service;
private int runStep=0;


	public void setService(pencil_service service){
		this.service= service;
	}
	
	holeMachineTO hmto ;

	@RequestMapping("/begin.do")
	public ModelAndView mixMachine(String orderNumS) throws SQLException, InterruptedException{
		//0. 주문량에 필요한 재료계산
				int namuNeed=Integer.parseInt(orderNumS);
				int order =  Integer.parseInt(orderNumS);
				int normal=0;//성공된 제품의수
				int abnormal=0;//비정상 품
				factoryMaterial2TO namuto = service.getNamuStore();
				factoryMaterial2TO ftoSet = new factoryMaterial2TO();
				int getnamupan =0;
				
				if(namuto!=null){
					getnamupan = namuto.getNamuPan();
				}
				int namuPan = getnamupan;
				
				hmto = service.selectHoleMInfo();
				if(hmto==null){
					hmto  = new holeMachineTO();//
				}
				
				
				
				
				int machineLife = hmto.getMachineLife();
				
				
				if(namuNeed <= namuPan){
					int num = namuPan-namuNeed;
					ftoSet.setNamuPan(num);
					ftoSet.setDateRegist(pencilUtil.getNow());//현재날짜
					ftoSet.setInvenInfo("재료 출고 : "  + namuNeed);
					service.insertNamuStore(ftoSet);
					System.out.println("재료를 가져왓슴 공정시작");
				}else{
					
					int  num = namuNeed - namuPan;
					System.out.println("재고량이 부족합니다.");
					ftoSet.setNamuPan(namuPan + num);
					ftoSet.setDateRegist(pencilUtil.getNow());//현재날짜
					ftoSet.setInvenInfo("재료 입고 : " + num);
					service.insertNamuStore(ftoSet);
					namuPan = service.getNamuStore().getNamuPan();
					if(namuNeed <= namuPan){
						
						int num1 = namuPan-namuNeed;
						
						ftoSet.setNamuPan(num1);
						ftoSet.setDateRegist(pencilUtil.getNow());//현재날짜
						ftoSet.setInvenInfo("재료 출고 : "  + namuNeed);
						service.insertNamuStore(ftoSet);
					
					}
					
					System.out.println("재료를 가져왓슴 공정시작");
					
				}
				
				
				
				
				hmto.setRunstep(pencilUtil.HolerunStep(namuNeed));
				
				double run = hmto.getRunstep();
				hmto.setStartTime(pencilUtil.getNow());//시작시간
				
				System.out.println(" 오더 나무판 수 : " + orderNumS);
				while(namuNeed!=0){
					
					
					
					hmto.setBoard(namuNeed);
					
					int testValue=pencilUtil.troubleMaker(5, 10);//불량률 조절 매서드(기본값, 랜덤값)
			//		System.out.println("불량률:"+testValue );
				
					if(testValue>=7){//불량률 판별
						normal=normal+1;//성공품 증가
						hmto.setState("정상");
					}else{
						abnormal=abnormal+1;
						hmto.setState("불량");
					}
				
					machineLife++;
					namuNeed--;
					System.out.println("나무판 개수 : " + namuNeed);
					System.out.println("정상품 개수 " + normal);
					System.out.println("불량품 개수 " + abnormal);
					System.out.println("진행률:"+(int)hmto.getRunstep());
					hmto.setRunstep(hmto.getRunstep() + run);
					
					if(hmto.getRunstep() >= 100){
						
						hmto.setRunstep(100);
					}
					
					
					
					runStep = (int) hmto.getRunstep();
					
					hmto.setNormal(normal);
					hmto.setMachineLife(machineLife);
					hmto.setEndTime(pencilUtil.getNow());
					hmto.setAbnormal(abnormal);
					
					//작동시간//
					Thread.sleep(2000);
					//
					
				}
				
				
				
				System.out.println(" 오더 나무판 수 : " + orderNumS);
				System.out.println(hmto);
				hmto.setBoard(order);
				service.insertHoleMInfo(hmto);
				
				
				
				holeNamuTO hnto = service.selectHoleNamu();
				if(hnto!=null){
					hnto.setAmount(hnto.getAmount() + hmto.getNormal());
					hnto.setInvenInfo("입고 : " + hmto.getNormal());
				}else{
					 hnto = new holeNamuTO(0, hmto.getNormal(), pencilUtil.getNow(), "입고 : " + hmto.getNormal());
				}
				
				
				service.insertHoleNamu(hnto);
		return null;
				
		 
	}//mix
	

	@RequestMapping("/getmoniser.do")
	public void bannerItem(HttpServletRequest request,HttpServletResponse response)throws Exception{
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("cache-control", "no-cache");
		System.out.println("제이슨 호출");
	
	///////////////////////////////////////////////
		
//		holeMachineTO htto1= service.selectHoleMInfo();
//		hmto.setRunstep(runStep);
		
		
		String responseMsg =null;
		JSONObject jobj= new JSONObject(hmto);
		responseMsg = jobj.toString();
		PrintWriter out = response.getWriter();
		System.out.println(responseMsg);
		out.print(responseMsg);
		return;
	}
	
	@RequestMapping("/getHoleNamu.do")
	public void getHoleNamu(HttpServletRequest request,HttpServletResponse response)throws Exception{
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("cache-control", "no-cache");
		System.out.println("깍은 나무개수 호출");
	
	///////////////////////////////////////////////
		
//		holeMachineTO htto1= service.selectHoleMInfo();
//		hmto.setRunstep(runStep);
		holeNamuTO hnto = service.selectHoleNamu();
	
		String responseMsg =null;
		JSONObject jobj= new JSONObject(hnto);
		responseMsg = jobj.toString();
		PrintWriter out = response.getWriter();
		System.out.println(responseMsg);
		out.print(responseMsg);
		return;
	}
	
	
	
}
